package com.gdqy.FCS.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.api.Fileupload;
import com.gdqy.FCS.entity.Design;
import com.gdqy.FCS.entity.DesignerInfo;
import com.gdqy.FCS.entity.DesignerLabel;
import com.gdqy.FCS.entity.Dictionary;
import com.gdqy.FCS.entity.Special;
import com.gdqy.FCS.entity.User;
import com.gdqy.FCS.service.DesignService;
import com.gdqy.FCS.service.DesignerInfoService;
import com.gdqy.FCS.service.DesignerLabelService;
import com.gdqy.FCS.service.DictionaryService;
import com.gdqy.FCS.service.InformationService;
import com.gdqy.FCS.service.SpecialService;
import com.gdqy.FCS.service.UserService;
import com.github.pagehelper.PageHelper;

@Controller
@RequestMapping("designer")
public class DesignerInfoController {
	
	@Autowired
	private DesignerInfoService designerInfoService;
	@Autowired
	private DesignService designService;
	@Autowired
	private SpecialService specialService;
	@Autowired
	private DictionaryService dictionaryService;

	@Autowired
	private UserService userService;
	@Autowired
	private DesignerLabelService designerLabelService;
	@Autowired
	private InformationService informationService;
	
	/************************************************************设计师信息**********************************************************************/
	//个人中心
	
	/**
	 * 
	 *作者：张伟键
	 *方法描述：
	 * @param user_id 用户id
	 * @return
	 */
	@RequestMapping("designer")
	public ModelAndView designer(@RequestParam(value = "user_id", required = true) int user_id){
		
		ModelAndView modelAndView = new ModelAndView();
			
				DesignerInfo designerInfo = designerInfoService.selectByUser_id(user_id);
				
				//如果设计师表中已存在
				if(designerInfo != null){
					int designer_id = designerInfo.getId();
				
					//设计师标签
					List<DesignerLabel> designerLabels =designerLabelService.selectByDesigner_id(designer_id);
					modelAndView.addObject("designerLabels", designerLabels);
					//专辑总数
					int special_count = specialService.selctCountByDesignerId(designer_id);
					modelAndView.addObject("special_count", special_count);
					//作品总数
					int design_count = designService.selectCountByDesignerId(designer_id);
					modelAndView.addObject("design_count", design_count);
					//获取改设计师发布资讯总数
					int information_count = informationService.selectCountByRelease_id(user_id);
					modelAndView.addObject("information_count", information_count);
					//设计师信息
					modelAndView.addObject("designerInfo", designerInfo);
					//跳转至设计师页面
					modelAndView.setViewName("designer/designer");
				}else{
					//没有该设计师
					modelAndView.addObject("msg", "不存在该设计师");
					modelAndView.setViewName("common/error");
				}
			
		
		
		
		return modelAndView;
	}
	
	
	
	
	/************************************************************账号管理相关**********************************************************************/
	//修改个人信息的页面跳转
	@RequestMapping("deal_changeInfo")
	public ModelAndView deal_changeInfo(int designer_id){
		ModelAndView modelAndView = new ModelAndView();
		//字典中的设计师标签
		List<Dictionary> dic_designerLabels = dictionaryService.selectByDicTypeName("设计标签");
		modelAndView.addObject("dic_designerLabels", dic_designerLabels);
		//设计师选择的设计标签
		List<DesignerLabel> designerLabels = designerLabelService.selectByDesigner_id(designer_id);
		modelAndView.addObject("designerLabels", designerLabels);
		//设计师信息
		DesignerInfo designerInfo = designerInfoService.selectById(designer_id);
		modelAndView.addObject("designerInfo", designerInfo);
		//设计师学历
		Dictionary education = dictionaryService.selectByCode(designerInfo.getEducation_code());
		modelAndView.addObject("education", education);
		modelAndView.setViewName("designer/changeInfo");
		return modelAndView;
		
	}
	
	/**
	 * 
	 *作者：张伟键
	 *方法描述：
	 * @param designerInfo  设计师基本信息
	 * @param designerLabels 设计师标签
	 * @param photo_pic 设计师头像
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	//修改设计师信息操作
	@RequestMapping("changeInfo")
	
	public ModelAndView changeInfo(DesignerInfo designerInfo,MultipartFile photo_pic,
			String[] designerLabels) throws IllegalStateException, IOException{
		User user = userService.selectById(designerInfo.getUser_id());
		designerInfo.setUpdate_time(new Date());
		String username= user.getUsername();
		String originalFilename =  photo_pic.getOriginalFilename();
		//头像处理
			//如果头像为空，不做处理,否则 修改
			if(photo_pic != null && originalFilename!=null && originalFilename!="" ){
				Fileupload fileupload = new Fileupload();
				String url=fileupload.upload(photo_pic, "designer", username, "designer-info");
				designerInfo.setPhoto_link(url);
				user.setPhoto_link(url);
				userService.update(user);
			}
		
		//个人资料处理
		
		 designerInfoService.update(designerInfo);
		//标签处理
			//删除现有标签
			int designer_id=designerInfo.getId();
			designerLabelService.deleteByDesiner_id(designer_id);
			//新增标签
			for(int temp=0;temp<designerLabels.length;temp++){
				DesignerLabel designerLabel = new DesignerLabel();
				designerLabel.setDesigner_id(designer_id);
				designerLabel.setPopularity(0);
				designerLabel.setLabel_code(designerLabels[temp]);
				designerLabelService.insert(designerLabel);
			}
			
		return new ModelAndView("redirect:/designer/deal_changeInfo.do?designer_id="+designer_id);
	}
	/**
	 * 
	 *作者：张伟键
	 *方法描述：因为设计师的信息已经包含在设计作品里面，
	 *所以按作品的受欢迎度查询之后也关联查询出设计师的信息
	 * @return
	 */
	//浏览设计师
	@RequestMapping("scan_designer")
	public ModelAndView scan_designer(){
		ModelAndView modelAndView  = new ModelAndView();
		//女装设计师
		List<Design> designs_lady = designService.selectFab_designer("CL002");
		modelAndView.addObject("desigs_lady", designs_lady);
		//男装设计师
		List<Design> designs_men = designService.selectFab_designer("CL001");
		modelAndView.addObject("designs_men", designs_men);
		//童装设计师
		List<Design> designs_child = designService.selectFab_designer("CL003");
		modelAndView.addObject("designs_child", designs_child);
		//内衣设计师
		List<Design> designs_underwear  = designService.selectFab_designer("CL006");
		modelAndView.addObject("designs_underwear", designs_underwear);
		//婚纱礼服设计师
		List<Design> design_wedding = designService.selectFab_designer("CL004");
		modelAndView.addObject("design_wedding", design_wedding);
		//毛衣针织
		List<Design> design_sweater = designService.selectFab_designer("CL005");
		modelAndView.addObject("design_sweater", design_sweater);
		//设计师人数
		int designer_count = designerInfoService.selectAllCount();
		modelAndView.addObject("designer_count", designer_count);
		modelAndView.setViewName("designer/designer_allList");
		
		return modelAndView;
	}
	
	@RequestMapping("scan_designerByType")
	public ModelAndView scan_designerByType() {
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.setViewName("designer/listByType");
		return modelAndView;
		
	}
	/**
	 * 
	 *作者：张伟键
	 *方法描述：此方法返回设计师的信息
	 * @param designer_id
	 * @return
	 */
	
	@RequestMapping("designer_introduction")
	public ModelAndView designer_introduction(@RequestParam(value="designer_id",required = true) int designer_id){
		ModelAndView modelAndView = new ModelAndView();
		DesignerInfo designerInfo = designerInfoService.selectById(designer_id);
		if(designerInfo != null){
			List<DesignerLabel> designerLabels = designerLabelService.selectByDesigner_id(designer_id);
			modelAndView.addObject("designerLabels", designerLabels);
			modelAndView.addObject("designerInfo", designerInfo);
			modelAndView.setViewName("designer/designer_introduction");
			
			List<Special> specials = specialService.selectByDesigner_id(designer_id);
			modelAndView.addObject("specials", specials);
			//作品数量
			int design_count = designService.selectCountByDesignerId(designer_id);
			modelAndView.addObject("design_count", design_count);
			//专辑数量
			int special_count = specialService.selctCountByDesignerId(designer_id);
			modelAndView.addObject("special_count", special_count);
		}else{
			modelAndView.addObject("msg", "不存在该设计师！");
			modelAndView.setViewName("common/error");
		}
		return modelAndView;
	}
	
	/**
	 * 
	 *作者：张伟键
	 *方法描述：
	 * @param type 设计的类型
	 * @return
	 */
	@RequestMapping("getDesingerByType")
	public ModelAndView getDesingerByType(String type) {
		ModelAndView modelAndView = new ModelAndView();
		List<Design> designs = designService.selectFab_designer(type);
		modelAndView.addObject("designs", designs);
		modelAndView.setViewName("designer/designer_list");
		return modelAndView;
	}
	
}
